<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * 词语过滤
 *
 * @category  Admin
 */
class WordAction extends AdminAction
{

	protected $Word;


	public function _initialize()
	{
		parent::_initialize();
		$this->Word = D('Word');
	}


	//词语列表
	public function wordList()
	{
		import('ORG.Util.Page');

		$total = $this->Word->count();
		$Page  = new Page($total);
		$pagination = $Page->show();
		$wordList = $this->Word->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('pagination', $pagination);
		$this->assign('wordList', $wordList);
		$this->display();
	}


	//导入词语
	public function importWord()
	{
		if ($this->isPost())
		{
		    $content = array();
		    if (is_uploaded_file($_FILES['file']['tmp_name']))
		    {
		    	$content = file($_FILES['file']['tmp_name']);
		    }
		    $importWords = array_unique(array_merge($content, explode("\n", $_POST['words'])));
			if (!empty($importWords))
			{
			    $oldWords = array();
			    $results = $this->Word->select();
			    if (!empty($results))
			    {
			        foreach ($results as $value)
			        {
			    	    $oldWords[md5($value['word'])] = $value['replacement'];
			        }
			    }

			    $override = intval($_POST['override']);
				foreach ($importWords as $value)
				{
					list($word, $replacement) = array_map('trim', explode('=', $value));
					$data = array(
						'word'        => $word,
						'replacement' => !empty($replacement) ? $replacement : '***',
					);
					if (strlen($word) < 3 || false === strpos($value, '='))
					{
					   // do nothing
					}
					elseif (isset($oldWords[md5($word)]))
					{
						if ($override)
						{
						    $where = array('word' => $value['word']);
							$this->Word->where($where)->save($data);
						}
					}
					else
					{
					    $this->Word->add($data);
					}
				}

			}

			$this->Word->cacheCensoredWords();
			$this->success(L('word.word_updated_succeed'));
		}
		else
		{
		    $boolRadios = array(1 => L('yes'), 0 => L('no'));

		    $this->assign('overrideRadios', $boolRadios);
		    $this->assign('overrideChecked', 0);

		    $this->assign('truncateRadios', $boolRadios);
		    $this->assign('truncateChecked', 0);

			$this->assign('rs', $wordInfo);
			$this->display();
		}
	}


	//导出词语
	public function exportWord()
	{
	    import('ORG.Net.Http');

        $filename = 'censored_wordlist.txt';
	    $words = $this->Word->select();
	    if (!empty($words))
	    {
	       $content = '';
	       foreach ($words as $value)
	       {
	    	  $content .= $value['word'].'='.$value['replacement']."\n";
	       }
	       Http::download($filename, '', $content);
	       exit();
	    }
	}


	//管理词语
	public function manageWord()
	{
		if ($this->isPost())
		{
		    if (!empty($_POST['mods']))
		    {
		        $where = array('word_id' => array('in', $_POST['mods']));
		    	$this->Word->where($where)->delete();
		    }

		    if (!empty($_POST['new_word']))
			{
			    if (strlen($_POST['new_word']) < 3)
			    {
			    	$this->error(L('word_too_short'));
			    }
			    $where = array('word' => $_POST['new_word']);
				$result = $this->Word->where($where)->find();
				if (!empty($result))
				{
					$this->error(L('word.word_existed'));
				}
				else
				{
				    $data = array(
				        'word'        => $_POST['new_word'],
				        'replacement' => empty($_POST['new_replacement']) ? '***' : $_POST['new_replacement'],
				    );
				    $this->Word->add($data);
				}
			}

			if (!empty($_POST['word']))
			{
				foreach ($_POST['word'] as $key => $value)
				{
				    if (strlen($value) > 3)
				    {
				        $data = array(
				            'word'        => $_POST['word'][$key],
				            'replacement' => empty($_POST['replacement'][$key]) ? '***' : $_POST['replacement'][$key],
				        );
				        $where = array('word_id' => $key);
				        $this->Word->where($where)->save($data);
				    }
				}
			}
			$this->Word->cacheCensoredWords();
			$this->success(L('word.word_updated_succeed'));
		}
	}

}
//End Class

/* End of file CensorWordAction.class.php */
/* Location: ./App/Lib/Action/Admin/WordAction.class.php */